package com.example.demo1.controller;

import com.example.demo1.client.Demo2Client;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/demo1")
@RequiredArgsConstructor
@Slf4j
public class Demo1Controller {
    private final Demo2Client demo2Client;

    @GetMapping("/hello")
    public String hello() {
        log.info("Demo1 /hello called - HOST_IP: {}", System.getenv("HOST_IP"));
        return "Hello Demo7";
    }
    @GetMapping("/call-demo2")
    public String callDemo2(HttpServletRequest request) {
        // 记录请求来源和调用目标
        log.info("Received call-demo2 request from: {}", request.getRemoteAddr());
        log.info("Attempting to call demo2 via Feign...");

        long startTime = System.currentTimeMillis();
        try {
            String result = demo2Client.helloFromDemo2();
            long duration = System.currentTimeMillis() - startTime;

            log.info("Successfully called demo2. Response time: {} ms", duration);
            return "Demo1 -> " + result;
        } catch (Exception e) {
            log.error("Failed to call demo2. Error: {}", e.getMessage(), e);
            throw e;
        }
    }
}
